package cn.edu.xjtu.nbrash.xbarsuite.terry;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.InputFilter;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.zxingdemo.RashDisplayActivity;

import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;

public class EncodeSMS extends Activity {

	private static final int PICK_CONTACT = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_encode_sms);
		final EditText phoneEdit = (EditText) findViewById(R.id.phone_text);

		phoneEdit.setFilters(new InputFilter[] { new InputFilter.LengthFilter(11) });

	}

	public void showQR(View v) {

		final EditText phoneEdit = (EditText) findViewById(R.id.phone_text);
		String input = phoneEdit.getText().toString();
		if(input.length()<11||input.charAt(0)!='1'){
			//提示用户输入的字符长度不够，可以用TOAST也可以用DIALOG
			
			Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_LONG).show();
			}
		else{
			
			String phoneNum = ((TextView) findViewById(R.id.phone_text)).getText()
					.toString();
			String content = ((TextView) findViewById(R.id.content_text)).getText()
					.toString();
			String data = phoneNum + "?body=" + content;

			if (phoneNum != null && !phoneNum.equals("")) {
				Intent intent = new Intent(this, RashDisplayActivity.class);
				// ACTION 是 ENCODE
				intent.setAction(Intents.Encode.ACTION);
				// 类型是TEXT，这里可以有很多中类型，具体参见QRCodeEncoder源码
				intent.putExtra(Intents.Encode.TYPE, Contents.Type.SMS);
				// 数据，这里是字符串
				intent.putExtra(Intents.Encode.DATA, data);
				startActivity(intent);
			}
		}
		
	}

	public void back(View v) {
		finish();
	}
	public void pickFromContact(View v) {

		Intent intent = new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT);

	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

		switch (reqCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c1 = managedQuery(contactData, null, null, null, null);

				if (c1.moveToFirst()) {
					String contactName = c1
							.getString(c1
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

					// TextView nameText = (TextView)
					// findViewById(R.id.name_text);
					// nameText.setText(contactName);

					String contactId = c1.getString(c1
							.getColumnIndex(ContactsContract.Contacts._ID));
					Cursor phone = getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ " = " + contactId, null, null);

					String number = "";
					while (phone.moveToNext()) {
						String strPhoneNumber = phone
								.getString(phone
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

						number += strPhoneNumber + " ";
					}

					TextView phoneText = (TextView) findViewById(R.id.phone_text);
					phoneText.setText(number);
				}
			}
			break;
		}
	}
	// public void showQR(View v) {
	// String phoneNum= ((TextView)
	// findViewById(R.id.phone_text)).getText().toString();
	// String content = ((TextView)
	// findViewById(R.id.content_text)).getText().toString();
	//
	// if(phoneNum != null && !phoneNum.equals("")){
	// Intent intent = new Intent(this, RashDisplayActivity.class);
	// Bundle bundle = new Bundle();
	//
	// // ACTION 是 ENCODE
	// intent.setAction(Intents.Encode.ACTION);
	// // 类型是TEXT，这里可以有很多中类型，具体参见QRCodeEncoder源码
	// intent.putExtra(Intents.Encode.TYPE, Contents.Type.);
	// // 数据，这里是字符串
	// bundle.putString(SomeNewConstant., phoneNum);
	// bundle.putString(SomeNewConstant.CONTENT, content);
	//
	// intent.putExtra(Intents.Encode.DATA,bundle);
	// startActivity(intent);
	// }
	// }

}
